/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xos_ffmpeg.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#ifndef __IMPL_SDL2_MEM_H__
#define __IMPL_SDL2_MEM_H__

namespace xos_ffmpeg
{

    class sdl2_mem
    {
    public:
        sdl2_mem();
        ~sdl2_mem();

    public:
        static int SDLCALL static_read_buffer( void * opaque, uint8_t * buf, int buf_size );
        static void SDLCALL static_fill_audio( void * udata, Uint8 * stream, int len );
        static int static_init();
        static int static_term();

    protected:
        int tanslate_audio_fmt();
        int sdl_msg_proc( const SDL_Event & event );
        int read_buffer( uint8_t * buf, int buf_size );
        int fill_audio( Uint8 * stream, int len );
        int update( bool bForceUpdate );

    protected:
        int receive_video_frame();
        int receive_audio_frame();
        int decode_all();

    protected:
        int helper_notify( int nMsg );
        int term_decode_objs();
        int init_data();

    public:
        int notify();
        int heart();
        int wait();
        int init();
        int term();

    protected:
        // 文件解码参数
        AVFormatContext * m_pFileFormatCtx;
        int m_nVideoStreamIndex;
        int m_nAudioStreamIndex;
        xos_box::i_big_buf * m_pBigBuf;
        xos_box::i_buf * m_pAvioBuf;
        AVIOContext * m_pAvioCtx;
        AVPacket * m_pPacket;
        AVFrame * m_pFrameYUV;
        AVFrame * m_pFrame;

        std::string m_input_file;

    protected:
        // 视频解码参数
        AVCodecContext * m_pVideoCodecCtx;
        const AVCodec * m_pVideoCodec;
        SwsContext * m_pVideoSwsCtx;

    protected:
        // 音频解码参数
        AVCodecContext * m_pAudioCodecCtx;
        const AVCodec * m_pAudioCodec;
        SwrContext * m_pAudioSwrCtx;

    protected:
        // 音频播放参数
        SDL_AudioSpec m_audio_obtained_spec;
        SDL_AudioSpec m_audio_desired_spec;
        int m_nAudioDeviceId;

    protected:
        // 公共播放参数
        xos_box::i_list * m_pFileDataList;
        xos_box::i_list * m_pVideoList;
        xos_box::i_list * m_pAudioList;
        bool m_bRunning;

    protected:
        // 视频播放参数
        SDL_Window * screen;
        SDL_Renderer * sdlRenderer;
        SDL_Texture * sdlTexture;
        uint8_t * buffer;

        int cur_screen_w;
        int cur_screen_h;
        int screen_w;
        int screen_h;

        //IYUV: Y + U + V  (3 planes)
        //YV12: Y + V + U  (3 planes)
        int pixformat;
        int pixel_w;
        int pixel_h;
        int bpp;

    };

} // xos_ffmpeg

#endif // __IMPL_SDL2_MEM_H__
